"""
序列化
"""
from rest_framework import serializers
from django.contrib.auth.models import User, Group


class UserSerializers(serializers.HyperlinkedModelSerializer):
    password = serializers.CharField(label="密码", write_only=True, help_text="密码不要太简单")

    class Meta:
        # model 指明序列化目标
        model = User
        # fields 指明序列化哪些字段
        fields = ["url", "username", "password"]

    def create(self, validated_data):
        username = validated_data.get("username")
        password = validated_data.get("password")
        instance = User.objects.create_superuser(username, password=password)
        return instance

    def update(self, instance, validated_data):
        instance.username = validated_data.get("username", instance.username)
        instance.set_password(validated_data.get("password"))
        instance.save()
        return instance


class GroupSerializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        # model 指明序列化目标
        model = Group
        # fields 指明序列化哪些字段
        fields = ["url", "id", "name"]


class UserListSerializers(serializers.ModelSerializer):
    """
    用户列表序列化
    """
    # url = serializers.HyperlinkedIdentityField(read_only=True, view_name="user-detail", lookup_field="pk")

    class Meta:
        model = User
        fields = ['username']


class UserCreateSerializers(serializers.Serializer):
    """
    创建用户序列化
    """
    id = serializers.IntegerField(label="id", read_only=True, help_text="用户id")
    username = serializers.CharField(label="用户名", help_text="输入用户名")
    password = serializers.CharField(label="密码", write_only=True, help_text="密码不要太简单")

    def create(self, validated_data):
        username = validated_data.get("username")
        password = validated_data.get("password")
        instance = User.objects.create_superuser(username, password=password)
        return instance


class UserRetrieveSerializers(serializers.ModelSerializer):
    """
    用户详情查询序列化
    """
    # url = serializers.HyperlinkedIdentityField(read_only=True, view_name="user-detail", lookup_field="pk")

    class Meta:
        model = User
        fields = "__all__"


class UserUpdatePasswordSerializers(serializers.Serializer):
    """
    用户更新密码序列化
    """
    username = serializers.CharField(label="用户名", read_only=True, help_text="输入用户名")
    password = serializers.CharField(label="密码", write_only=True, help_text="密码不要太简单")

    def update(self, instance, validated_data):
        instance.set_password(validated_data.get("password"))
        instance.save()
        return instance


class UserSerializers(serializers.Serializer):
    """
    普通序列化
    """
    url = serializers.HyperlinkedIdentityField(read_only=True, view_name="user-detail", lookup_field="pk")
    id = serializers.IntegerField(label="id", read_only=True, help_text="用户id")
    username = serializers.CharField(label="用户名", help_text="输入用户名")
    password = serializers.CharField(label="密码", write_only=True, help_text="密码不要太简单")

    def create(self, validated_data):
        username = validated_data.get("username")
        password = validated_data.get("password")
        instance = User.objects.create_superuser(username, password=password)
        return instance

    def update(self, instance, validated_data):
        instance.username = validated_data.get("username", instance.username)
        instance.set_password(validated_data.get("password"))
        instance.save()
        return instance


class GroupSerializers(serializers.Serializer):
    url = serializers.HyperlinkedIdentityField(read_only=True, view_name="group-detail", lookup_field="pk")
    name = serializers.CharField(label="用户组名", help_text="输入用户组名")

    def create(self, validated_data):
        instance = Group.objects.create(**validated_data)
        return instance

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name", instance.name)
        instance.save()
        return instance
